<?php

namespace Admin\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Admin\Form\User\LoginForm;
use Admin\Form\User\FilterLoginForm;
use Zend\Authentication\Adapter\DbTable;
use Zend\Authentication\AuthenticationService;

class UserController extends AbstractActionController {

    const table_user = 'zend_users';

    private $objView;
    private $authService;

    public function __construct() {

        $this->objView = new ViewModel();

        $this->authService = new AuthenticationService();
    }
    
    public function onDispatch(\Zend\Mvc\MvcEvent $e) {
        $this->layout('admin/layout');
        return parent::onDispatch($e);
    }

    public function loginAction() {

        $session = new \Zend\Session\Container('user');

        if ($session->offsetExists('name')) {
            $this->redirect()->toRoute('product-admin/paginator');
        }

        $loginForm = new LoginForm();

        $request = $this->getRequest();

        if ($request->isPost()) {

            $postData = $request->getPost()->toArray();

            $loginForm->setInputFilter(new FilterLoginForm());

            $loginForm->setData($postData);

            if ($loginForm->isValid()) {

                $userData = $loginForm->getData();

                $dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');

                $authAdapter = new DbTable($dbAdapter, self::table_user);
                //set column in databse to compare    
                $authAdapter->setIdentityColumn('user_name');
                $authAdapter->setCredentialColumn('user_password');
                //get data from post to compare
                $authAdapter->setIdentity($userData['user_name']);
                $authAdapter->setCredential(\Library\String::createPassword($userData['user_password']));

                $result = $this->authService->authenticate($authAdapter);
                //if matched
                if ($result->isValid()) {

                    $arySession = $authAdapter->getResultRowObject(array('user_id', 'user_full_name', 'user_email'));

                    $session->offsetSet('name', $arySession->user_full_name);
                    $session->offsetSet('email', $arySession->user_email);
                    if ($userData['remember_me'] == 1) {
                        $sessionManager = new \Zend\Session\SessionManager();
                        $sessionManager->rememberMe(302400); // 7 days
                    }
                    $this->redirect()->toRoute('product-admin/paginator');
                } else {
                    $message = 'Login fail';
                    $this->objView->setVariable('message', $message);
                }
            }
        }

        $this->objView->setVariable('loginForm', $loginForm);

        $this->objView->setTemplate('admin/user/login');

        return $this->objView;
    }

    public function logoutAction() {

        $sessionManager = new \Zend\Session\SessionManager();
        $sessionManager->forgetMe();

        $userSession = new \Zend\Session\Container('user');
        $userSession->getManager()->destroy();

        $this->redirect()->toRoute('user-admin');
    }

    public function getAutthenticationService() {
        return $this->getServiceLocator()->get('AdminAuthenticationService');
    }

}
